//
//  SYRootViewController.m
//  装修管家
//
//  Created by 千锋 on 16/1/20.
//  Copyright (c) 2016年 IOS1503薛凯. All rights reserved.
//

#import "SYRootViewController.h"
#import "AFNetworking.h"
#import "SYADTableViewCell.h"
#import "SYMenuTableViewCell.h"
#import "Header.h"
#import "SYFirstModel.h"
#import "SYSecondModel.h"
#import "SYTopicTableViewCell.h"
#import "TaoTuTableViewCell.h"
@interface SYRootViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property (strong,nonatomic)UITableView* tableview;
//广告栏数据
@property (strong,nonatomic)NSMutableArray* ADDataArray;
//导航栏
@property (strong,nonatomic)UIView* navview;
//数据
@property (strong,nonatomic)NSMutableArray* FirdataArray;
@property (strong,nonatomic)NSMutableArray* SecdataArray;
@end

@implementation SYRootViewController
#pragma mark- 页面生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=YES;
    [self createNav];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self getADData];
    [self getMoreData];
}
#pragma mark- 导航栏
- (void)createNav{
    _navview=[[UIView alloc] initWithFrame:CGRectMake(0, 20, DEVICEWIDTH, 40)];
    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 20)];
    label.text=@"土巴兔—装修管家";
    [_navview addSubview:label];
    UIButton* button=[[UIButton alloc] initWithFrame:CGRectMake(DEVICEWIDTH-40, 10, 20, 20)];
    [button setBackgroundImage:[UIImage imageNamed:@"icon_home_tell"] forState:UIControlStateNormal];
    [_navview addSubview:button];
    [self.view addSubview:_navview];
}
- (void)createTableView{
    _tableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 60, DEVICEWIDTH, DEVICEHEIGHT-60)];
    [self.view addSubview:_tableview];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    [_tableview registerClass:[SYADTableViewCell class] forCellReuseIdentifier:@"SYADTableViewCell"];
    [_tableview registerNib:[UINib nibWithNibName:@"SYMenuTableViewCell" bundle:nil] forCellReuseIdentifier:@"SYMenuTableViewCell"];
    [_tableview registerNib:[UINib nibWithNibName:@"SYTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"SYTopicTableViewCell"];
    [_tableview registerNib:[UINib nibWithNibName:@"TaoTuTableViewCell" bundle:nil] forCellReuseIdentifier:@"TaoTuTableViewCell"];
}
#pragma mark- 表格协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2+_FirdataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        SYADTableViewCell* cell =[tableView dequeueReusableCellWithIdentifier:@"SYADTableViewCell"];
        [cell relayoutWithArrary:_ADDataArray];
        
        return cell;
    }else if (indexPath.row==1){
        SYMenuTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"SYMenuTableViewCell"];
        return cell;
    }else if([_FirdataArray[indexPath.row-2] isKindOfClass:[SYFirstModel class]]){
        SYTopicTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"SYTopicTableViewCell"];
        [cell relayoutWithModel:_FirdataArray[indexPath.row-2]];
        return cell;
    }else{
        TaoTuTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"TaoTuTableViewCell"];
        [cell relayoutWithModel:_FirdataArray[indexPath.row-2]];
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==0) {
        return DEVICEHEIGHT/4;
        
    }else if (indexPath.row==1){
        return DEVICEHEIGHT/6;
    }else if([_FirdataArray[indexPath.row-2] isKindOfClass:[SYSecondModel class]]){
        return 320;
        
    }else{
        return 230;
    }
}
#pragma mark- 获取数据
- (void)getADData{
    _ADDataArray=[[NSMutableArray alloc] initWithCapacity:0];
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString* urlStr=@"http://mobileapi.to8to.com/index.php?module=ad&action=get&appname=%E8%A3%85%E4%BF%AE%E7%AE%A1%E5%AE%B6&appid=1&cityName=&to8to_token=&addtype=2&systemversion=22&imei=867992026945527&appostype=1&appversion=2.5.6&version=2.5&channel=%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83&model=ad&";
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray* arr=[dic objectForKey:@"data"];
        for (NSDictionary* item in arr) {
            [_ADDataArray addObject:[item objectForKey:@"imgurl"]];
            [_tableview reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
- (void)getMoreData{
    _FirdataArray=[[NSMutableArray alloc] initWithCapacity:0];
    _SecdataArray=[[NSMutableArray alloc] initWithCapacity:0];
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString* urlStr=@"http://mobileapi.to8to.com/smallapp.php";
    NSDictionary* dic=@{@"systemversion":@"22",@"imei":@"867992026945527",@"appostype":@"1",@"action":@"gjlist",@"appversion":@"2.5.6",@"version":@"2.5",@"appid":@"1",@"to8to_token":@"",@"page":@"1",@"channel":@"%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83",@"model":@"homepage"};
    [manager POST:urlStr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray* array=[dic objectForKey:@"data"];
        NSArray* array1=[array[0] objectForKey:@"info"];
        //添加第一种数据
        for (NSDictionary* item in array1) {
            SYFirstModel* model=[[SYFirstModel alloc] initWithDic:item];
            model.type=@"1";
            [_FirdataArray addObject:model];
            [self.tableview reloadData];
        }
        //添加第二种数据
        NSArray* array2=[array[1] objectForKey:@"info"];
        for (NSDictionary* item2 in array2) {
            SYSecondModel* model2=[[SYSecondModel alloc] initWithDic:item2];
            model2.type=@"2";
            [_FirdataArray addObject:model2];
            [self.tableview reloadData];
        }
            
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark- scrollview协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}
@end
